//
//  FocusPostViewController.m
//  唧歪
//
//  Created by ZC on 15/9/2.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "FocusPostViewController.h"
#import "AppDelegate.h"
#import "myPost.h"
#import "focusPostCell.h"
#define PostURL @"https://app.jiwaipro.com:8888/index.php/JiWai/seek_up"
#define browseNumberURL @"https://app.jiwaipro.com:8888/index.php/JiWai/browse"  //浏览数

@interface FocusPostViewController ()

@end

@implementation FocusPostViewController{
    AppDelegate *app;
    NSMutableArray *array;
    NSMutableArray *postArray;
    int pageNumber;
    NSString *browseNumberStr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"他的帖子";
    array = [NSMutableArray array];
    postArray = [NSMutableArray array];
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    pageNumber = 1;
    [app isConnectionAvailable];
    if ([app.Existence isEqualToString:@"1"]) {
    }else{
    app.Existence = @"0";
    NSString *string = [NSString stringWithFormat:@"user_id=%@&pg=%d",app.myFocus_id,pageNumber];
    [self MyFocusPath:PostURL Str:string];
    }
}

-(void)MyFocusPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        array =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:array waitUntilDone:YES];
    }];
}
- (void)refreshData:(NSMutableArray *)sender{
    for (int i=0; i<(sender.count-1); i++) {
        myPost *p = [[myPost alloc]init];
        p.post_content = sender[i][@"message_content"];
        p.post_time = sender[i][@"message_time"];
        p.post_showTime = sender[i][@"message_showtime"];
        p.post_id=sender[i][@"message_id"];
        p.post_tag=sender[i][@"message_tag"];
        p.post_commentNumber=sender[i][@"message_comment"];
        
        [postArray addObject:p];
    }
    [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return postArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UINib *nib=[UINib nibWithNibName:@"focusPostCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"focusPostCell"];
    
    static NSString *cellIdentifier=@"focusPostCell";
    focusPostCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    myPost *myp= postArray[indexPath.row];
    
    cell.time.text=[NSString stringWithFormat:@"%@",myp.post_time];  //发布时间
    cell.number.text=[NSString stringWithFormat:@"%@",myp.post_commentNumber];  //回帖数
    
    if ([myp.post_tag isEqualToString:@"0"]) {
        cell.content.hidden = NO;
        cell.content.text=[NSString stringWithFormat:@"%@",myp.post_content];
        cell.content.textColor = [UIColor blackColor];
        cell.audioView.hidden = YES;
        
    }else if ([myp.post_tag  isEqualToString:@"1"]){
        cell.content.hidden = NO;
        cell.content.text = @"图片帖子";
        cell.content.textColor = [UIColor brownColor];
        cell.audioView.hidden = YES;
        
    }else{
        cell.content.hidden = YES;
        cell.audioView.hidden = NO;
        
    }
    cell.messageTag = [NSString stringWithFormat:@"%@",myp.post_tag];
    
    return cell;
    
}

#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    myPost *myPost=postArray[indexPath.row];
    self.messageID = [NSString stringWithFormat:@"%@",myPost.post_id];
    app.appmessageid = self.messageID;
    
    NSString *poststr =[NSString stringWithFormat:@"message_id=%@",app.appmessageid];
    [self BrowseNumberPath:browseNumberURL Str:poststr];
    
    PostDetailsViewController *postDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"detailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];
    
    
    
}

-(void)BrowseNumberPath:(NSString *)path Str:(NSString *)str{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        browseNumberStr =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshDataBrowse:) withObject:browseNumberStr waitUntilDone:YES];
    }];
}
-(void)refreshDataBrowse:(NSString *)str{
    
}

@end
